<?php
/**
 * @author Jerry
 */
class Router
{
	private $_controller;
	
	private $_action;
	
	private $_params;
	
	private $_param;
	
	private $_request = null;
	
	private $_route;

	public function __construct()
	{
		$this->_request = Arr::get($_REQUEST, 'param');
	}
	
	public function init(array $params)
	{
		$role = Arr::get($params, 'rule');
		$_role_type = Arr::get($params, 'type');
		$this->_route = Arr::get($params, 'default');
		$this->parseRole($_role_type, $role);
	}
	
	private function parseRole($_type, $role)
	{
		switch ($_type)
		{
			case 'reg':
				$this->_regRole();
				//TODO 正则表达式的规则
				break;
			case 'index':
				$this->_indexRole();
				//TODO 索引式规则
				break;
			default:
				$this->_defaultRole($role);
				break;
		}
	}
	
	/**
	 * <controller>-<action>-<params>.html
	 * 
	 */
	private function _defaultRole($role)
	{
		$role = pathinfo($role);
		$role = Arr::get($role, 'filename');
		$role = explode('-', $role);
		
		$this->_request = pathinfo($this->_request);
		$this->_request = Arr::get($this->_request, 'filename');
		$this->_request = !empty($this->_request) ? explode('-', $this->_request) : array();
		$_role = array();
		if (!empty($this->_request))
		{
			foreach ($this->_request as $keys => $value)
			{
				$_key = Arr::get($role, $keys);
				if (!$_key) 
				{
					$this->_params[] = $value;
					continue;
				}
				$_role[$_key] = $value; 
			}
		}
		$_defult_ctr_index 		= Arr::get($this->_route, 'controller');
		$_default_act_index 	= Arr::get($this->_route, 'action');
		$_default_param_index 	= Arr::get($this->_route, 'param');
		
		$this->_controller 	= Arr::get($_role, $_defult_ctr_index, 'index');
		$this->_action 		= Arr::get($_role, $_default_act_index, 'index');
		$this->_param 		= Arr::get($_role, $_default_param_index);
		
	}
	
	private function _indexRole()
	{	
		
	}
	
	private function _regRole()
	{
		
	}
	
	public function getAction()
	{
		return $this->_action;
	}
	
	public function getController()
	{
		return $this->_controller;
	}
	
	public function getParams()
	{
		return $this->_params;
	}
	
	public function getParam()
	{
		return $this->_param;
	}
}
?>
